package com.haust.cart.interceptor;


import org.apache.catalina.connector.Response;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class CartInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //true-->如果session存在，直接返回，不存在返回新Session
        //false-->如果session存在，直接返回，不存在返回null
        HttpSession session =
                request.getSession(); //默认不传参 为false
        Object loginUser = session.getAttribute("loginUser");

        if (loginUser == null) {
            response.sendError(Response.SC_FORBIDDEN);
            return false;
        }
        return true;
    }
}
